package com.smdk.dsminio.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smdk.dsminio.entity.DsminioFolderInfo;
import com.smdk.dsminio.service.DsminioBucketInfoService;
import com.smdk.dsminio.service.DsminioFileInfoService;
import com.smdk.dsminio.service.DsminioFolderInfoService;
import com.smdk.dsminio.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author 神秘的凯
 * @since 2020-03-19
 */

@Api(tags = {"查询对象集合"})
@Controller
@RequestMapping("/api")
public class DsminioFolderInfoController {
    @Autowired
    private DsminioBucketInfoService bucketinfoService;
    @Autowired
    private DsminioFileInfoService dsminioFileInfoService;
    @Autowired
    private DsminioFolderInfoService dsminioFolderInfoService;

    /**
     * 创建ObjectList
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/createFolderInfo", method = RequestMethod.POST)
    @ApiOperation(value = "创建文件夹")
    public @ResponseBody
    AjaxResult createObjectList(@ApiParam(value = "folderName", required = true) @RequestParam(value = "folderName") String folderName,
                                @ApiParam(value = "bucketId", required = true) @RequestParam(value = "bucketId") Long bucketId,
                                @ApiParam(value = "parentFolderId", required = true) @RequestParam(value = "parentFolderId") Long parentFolderId, HttpServletRequest request) {
        //保存进数据库
        DsminioFolderInfo dsminioFolderInfo =new DsminioFolderInfo();
        dsminioFolderInfo.setBucketId(bucketId);
        dsminioFolderInfo.setCreateDataTime(new Date());
        dsminioFolderInfo.setFolderName(folderName);
        dsminioFolderInfo.setFolderOrder(0);
        dsminioFolderInfo.setParentFolderId(parentFolderId);
        dsminioFolderInfo.setCreateDataTime(new Date());
        return AjaxResult.addResult(dsminioFolderInfoService.addDsminioFolderInfo(dsminioFolderInfo));
    }


    /**
     * 删除ObjectList
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteFolderInfo", method = RequestMethod.POST)
    @ApiOperation(value = "删除文件夹")
    public @ResponseBody
    AjaxResult deleteObjectList(@ApiParam(value = "bucketId", required = true) @RequestParam(required = true, value = "objectListId") Long objectListId) {
        DsminioFolderInfo dsminioFolderInfo = dsminioFolderInfoService.getById(objectListId);
        if (dsminioFolderInfo == null) {
            return AjaxResult.fail("当前文件夹不存在");
        }
        return AjaxResult.fail("删除失败");

    }


    /**
     * 修改ObjectList
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/updateFolderInfo", method = RequestMethod.POST)
    @ApiOperation(value = "修改文件夹名称")
    public @ResponseBody
    AjaxResult updateObjectList(@ApiParam(value = "folderId", required = true) @RequestParam(required = true, value = "folderId") Long folderId,
                                @ApiParam(value = "folderName", required = true) @RequestParam(required = true, value = "folderName") String folderName) {
        DsminioFolderInfo dsminioFolderInfo = new DsminioFolderInfo();
        dsminioFolderInfo.setFolderName(folderName);
        dsminioFolderInfo.setFolderId(folderId);
        return AjaxResult.updateResult(dsminioFolderInfoService.updateDsminioFolderInfo(dsminioFolderInfo));
    }


    /**
     * 查询ObjectList
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/searchObjectList", method = RequestMethod.GET)
    @ApiOperation(value = "查询ObjectList")
    public @ResponseBody
    AjaxResult searchObjectList(@ApiParam(value = "分页参数", required = true) @RequestParam(required = true, value = "startPage", defaultValue = "0") Integer startPage,
                                @ApiParam(value = "查询类型(0bucket 1ObjectList)", required = true) @RequestParam(required = true, value = "searchType") Integer searchType,
                                @ApiParam(value = "bucketId或者ObjectListID", required = true) @RequestParam(required = true, value = "bucketIdOrObjectListTopId") Long bucketIdOrObjectListTopId) {

        if (searchType == 0) {
            PageHelper.startPage(startPage, 10);
            return AjaxResult.updateResult(new PageInfo<>(dsminioFolderInfoService.searchDsminioFolderInfoByBucketId(bucketIdOrObjectListTopId)));
        } else {
            PageHelper.startPage(startPage, 10);
            return AjaxResult.updateResult(new PageInfo<>(dsminioFolderInfoService.searchDsminioFolderInfo(bucketIdOrObjectListTopId)));
        }
    }
}



